package lol.clann.minecraft.springboot.plugin.command;

import com.ruoshui.utils.java.string.StringUtils;
import lol.clann.minecraft.springboot.api.annotation.command.Command;
import lol.clann.minecraft.springboot.api.annotation.command.CommandDefine;
import lol.clann.minecraft.springboot.api.annotation.command.Optional;
import lol.clann.minecraft.springboot.api.annotation.command.Sender;
import lol.clann.minecraft.springboot.api.bukkit.command.TabComplete;
import lol.clann.minecraft.springboot.api.service.FlagService;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.command.CommandSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 操作flag的指令
 *
 * @author pyz
 * @date 2019/5/6 11:57 PM
 */
@Slf4j
@CommandDefine(name = "springboot", mapping = "flag")
public class FlagCommand {

    @Autowired
    private FlagService flagService;

    @Command(showArgs = "flag set <key> <value>", des = "设置全局flag", tabComplete = FlagKeyTabComplete.class)
    private boolean set(@Sender CommandSender sender, String key, @Optional String value) {
        String replace = flagService.set(key, value);
        sender.sendMessage(String.format("set falg %s=%s. replace value=%s", key, value, replace));
        return true;
    }

    @Command(showArgs = "flag get <key>", des = "获取全局flag", tabComplete = FlagKeyTabComplete.class)
    private boolean get(@Sender CommandSender sender, String key) {
        String value = flagService.get(key);
        sender.sendMessage("get " + key + "=" + value);
        return true;
    }

    @Command(showArgs = "flag allKeys", des = "查询所有的key")
    private boolean allKeys(@Sender CommandSender sender) {
        List<String> keys = flagService.getAllFlagKeys();
        sender.sendMessage(StringUtils.join(" , ", keys));
        return true;
    }

    @Command(showArgs = "flag allFlags", des = "查询所有的flag")
    private boolean allFlags(@Sender CommandSender sender) {
        Map<String, String> flags = flagService.getAllFlags();
        sender.sendMessage(StringUtils.join("\n", flags.entrySet(), e -> e.getKey() + "=" + e.getValue()));
        return true;
    }

    @Component
    public static class FlagKeyTabComplete implements TabComplete {
        @Autowired
        private FlagService flagService;

        @Override
        public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
            if (args.length != 3) {
                return new ArrayList<>();
            }
            String prefix = args[args.length - 1].toLowerCase();
            List<String> keys = flagService.getAllFlagKeys();
            List<String> candidates = new ArrayList<>();
            for (String key : keys) {
                if (key.toLowerCase().startsWith(prefix)) {
                    candidates.add(key);
                }
            }
            return candidates;
        }
    }

}
